package pokeronlab_2014.mcts;

import java.util.prefs.InvalidPreferencesFormatException;

import com.biotools.meerkat.Card;

import pokeronlab_2014.Controller;
import pokeronlab_2014.SVMClassifierData;
import pokeronlab_2014.mcts.TreeNode.NodeType;



public class CardNode extends TreeNode{

	
	public CardNode(){
		//Controller.debugWindow.printMessage("CardNode.CardNode");
		parent = null;
		SVMbemenet = new SVMClassifierData();
		typeOfNode = NodeType.newcard;
		nVisits = 0;
		totValue = 0;
	}
	
	public TreeNode selectNoFold(TreeNode currentNode){
		Controller.debugWindow.printMessage("ide nem kéne bekerülini:CardNode.selectNoFold");
		return currentNode;
	}
	
	public  void expand(TreeNode currentNode){
		//Controller.debugWindow.printMessage("CardNode.expand");
		children = new TreeNode[nCard];
		int childrennumber = 52;
//		if (Controller.thisGameInfo.isFlop())
//			childrennumber = nCard - 5;
//		if (Controller.thisGameInfo.isTurn())
//			childrennumber = nCard - 6;
		
		
		
		
		for (int i = 0; i < childrennumber; i++) {
			if(rules.lastaction==rules.myseat)
				children[i] = new BotNode();
			else
				children[i] = new OpponentNode();
			children[i].creatNode(this,i+100, currentNode.rules.getPlayerID(currentNode.rules.nextplayer(currentNode.rules.actualplayer, i+100)), 0, level);
		}
	}
	
	public TreeNode select(TreeNode currentNode){
		//Controller.debugWindow.printMessage("CardNode.select");
		
		//currentNode.typeOfNode= NodeType.newcard;
	
	
		if (currentNode.isLeaf())
			currentNode.expand(currentNode);
	
	
		deck.extractCard(deck.getCard(rules.elsolap));
		deck.extractCard(deck.getCard(rules.masodiklap));

		deck.extractCard(rules.flop1);
		deck.extractCard(rules.flop2);
		deck.extractCard(rules.flop3);
			
		if (rules.round>1){
			//Controller.debugWindow.printMessage("turn" + rules.turn + "round" + rules.round);
			deck.extractCard(rules.turn);
		}// meg kell nézni honnét kezdődik az indexelés
		
		


		Card kartya= deck.extractRandomCard();
		

		currentNode = currentNode.children[kartya.getIndex()];
		
		deck.replaceCard(deck.getCard(rules.elsolap));
		deck.replaceCard(deck.getCard(rules.masodiklap));
		deck.replaceCard(deck.getCard(rules.flop1));
		deck.replaceCard(deck.getCard(rules.flop2));
		deck.replaceCard(deck.getCard(rules.flop3));
		
		if (rules.round>1){
			deck.replaceCard(deck.getCard(rules.turn));
		}
		
		deck.replaceCard(kartya);
		//currentNode.creatNode(this, kartya.getIndex()+100, currentNode.rules.getPlayerID(currentNode.rules.nextplayer(currentNode.rules.actualplayer, kartya.getIndex()+100)), 0, level);
		
		
		
		//túlindexelés lehet TODO
		return currentNode;
	}

	public TreeNode simulate( TreeNode currentNode){
		//currentNode.typeOfNode= NodeType.newcard;
		//Controller.debugWindow.printMessage("CardNode.simulate");
		
		if (currentNode.isLeaf())
			currentNode.expand(currentNode);
		
		//rules.lastaction = Controller.thisGameInfo.getSmallBlindSeat();
		
		
		deck.extractCard(deck.getCard(rules.elsolap));
		deck.extractCard(deck.getCard(rules.masodiklap));
		for (int i = 0; i < 3; i++)
			//itt a board kártyákat lekérő fv javítva az újabbra
			deck.extractCard(Controller.thisGameInfo.getBoard().getCard(i));
		if (Controller.thisGameInfo.isRiver())
			deck.extractCard(Controller.thisGameInfo.getBoard().getCard(3));
		if (rules.turn!=-1)
			deck.extractCard(rules.turn);
		// meg kell nézni honnét kezdődik az indexelés

		int randomlap = (int) Math.ceil(Math.random() * 50)+1;
		Card randomCard = new Card(randomlap);

		while (!deck.inDeck(randomCard)) {
			randomlap = (int) Math.ceil(Math.random() * 50)+1;
			randomCard.setIndex(randomlap);
		}
		deck.extractCard(randomCard);
		currentNode = currentNode.children[randomlap];


		return currentNode;

	}


}
